package com.chenjun.web.controller.activiti;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModelUnPaginationInterceptor;
import com.chenjun.common.util.DateJsonValueProcessor;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/process/deploy")
public class DeployController {
	
	@Resource
	private RepositoryService repositoryService;
	
	/**
	 * 页面展示
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("activiti/process/deploy/list");
		return mv;
	}
	
	/**
	 * 分页查询流程
	 * @param datagrid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	public void getDeployPage(DataGridModelUnPaginationInterceptor datagrid, HttpServletResponse response) throws Exception{
		int rows = datagrid.getRows();
		int page = datagrid.getPage();
		int start = datagrid.getStart();
		Map<String, Object> params = datagrid.getParams();
		String processName = params.isEmpty() ? "" : params.get("processName").toString();
		
		ResultDto rtDto = new ResultDto();
		//当前页
		int pageNo = start / rows;
		//取得总数量
		long deployCount=repositoryService.createDeploymentQuery().deploymentNameLike("%"+processName+"%")
				.count();
		
		List<Deployment> deployList=repositoryService.createDeploymentQuery()//创建流程查询实例
				.orderByDeploymenTime().desc()  //降序
				.deploymentNameLike("%"+processName+"%")   //根据Name模糊查询
				.listPage(pageNo, rows);
		
		Pagination data = new Pagination();
		data.setAaData(deployList);
		data.setiTotalDisplayRecords((int)deployCount);
		data.setiTotalLongRecords(deployCount);
		data.setiTotalRecords((int)deployCount);
		data.setsEcho(page);
		rtDto.setData(data);
		
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.setExcludes(new String[]{"resources"});
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
		JSONObject result= JSONObject.fromObject(rtDto, jsonConfig);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
	}
	/**
	 * 添上传流程部署ZIP文件
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/addDeploy")
	@ResponseBody
	public ResultDto addDeploy(MultipartFile deployFile) throws Exception{
		ResultDto rtDto = new ResultDto();
		repositoryService.createDeployment() //创建部署
		.name(deployFile.getOriginalFilename())	//需要部署流程名称
		.addZipInputStream(new ZipInputStream(deployFile.getInputStream()))//添加ZIP输入流
		.deploy();//开始部署
		return rtDto;
	}

	/**
	 * 删除流程
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delDeploy(@PathVariable String id) throws Exception{
		ResultDto rtDto = new ResultDto();
		repositoryService.deleteDeployment(id, true);
		return rtDto;
	}

}
